//
//  XInComeViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/22.
//

#import "XInComeViewController.h"
#import "RSVerButton.h"
#import "XDynamicModel.h"

@interface XInComeViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)MyLinearLayout *contentLayout;


@property(nonatomic,strong)MyFlowLayout *flowLayout;

@property(nonatomic,strong)UIImageView* typeIcon;

@property(nonatomic,strong)UILabel* typeLb;

@property(nonatomic,strong)UITextField* amtTextField;

@property(nonatomic,strong)UITextField* remarkTextField;

/// 按钮信息
@property(nonatomic,strong)NSArray* btnInfos;

/// 按钮信息
@property(nonatomic,strong)NSArray* btns;

/// 收支信息
@property(nonatomic,strong)NSDictionary* info;

@end

@implementation XInComeViewController

- (void)loadView{
    [super loadView];
    
    // 线性布局
    self.view.backgroundColor = COLOR_WHITE;
    
    MyLinearLayout *rootLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    rootLayout.myMargin = 0;
    [self.view addSubview:rootLayout];

    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [rootLayout addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initBtnInfos];
    
    [self initViews];
    
    [self choiseType:self.btns.firstObject];

    // Do any additional setup after loading the view.
}

- (void)initBtnInfos{
    NSMutableArray* infos = [NSMutableArray array];
    [infos addObject:@{@"title":@"薪资",@"image_n":@"x_income_xz_n",@"image_h":@"x_income_xz_h",@"type":@(XFeeTypeXZ)}];
    [infos addObject:@{@"title":@"红包",@"image_n":@"x_income_hb_n",@"image_h":@"x_income_hb_h",@"type":@(XFeeTypeHB)}];
    [infos addObject:@{@"title":@"奖金",@"image_n":@"x_income_jj_n",@"image_h":@"x_income_jj_h",@"type":@(XFeeTypeJJ)}];
//    [infos addObject:@{@"title":@"报销",@"image_n":@"x_income_bx_n",@"image_h":@"x_income_bx_h",@"type":@(XFeeTypeBX)}];
//    [infos addObject:@{@"title":@"股票",@"image_n":@"x_income_lc_n",@"image_h":@"x_income_lc_h",@"type":@(XFeeTypeLC)}];
//    [infos addObject:@{@"title":@"退款",@"image_n":@"x_income_tk_n",@"image_h":@"x_income_tk_h",@"type":@(XFeeTypeTK)}];
//    [infos addObject:@{@"title":@"投资",@"image_n":@"x_income_tz_n",@"image_h":@"x_income_tz_h",@"type":@(XFeeTypeTZ)}];
    [infos addObject:@{@"title":@"兼职",@"image_n":@"x_income_jz_n",@"image_h":@"x_income_jz_h",@"type":@(XFeeTypeJZ)}];
    [infos addObject:@{@"title":@"稿酬",@"image_n":@"x_income_gc_n",@"image_h":@"x_income_gc_h",@"type":@(XFeeTypeGC)}];
    [infos addObject:@{@"title":@"其他",@"image_n":@"x_income_qt_n",@"image_h":@"x_income_qt_h",@"type":@(XFeeTypeQT1)}];
    _btnInfos = infos;
}

- (void)initViews{
    
    // 流式布局
    MyFlowLayout *flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.subviewVSpace = 16;
    flowLayout.subviewHSpace = 16;
    flowLayout.gravity = MyGravity_Horz_Fill;
    flowLayout.padding = UIEdgeInsetsMake(20, 20, 20, 20);
    flowLayout.myMargin = 0;
    [flowLayout setSubviewsSize:50 minSpace:16 maxSpace:MAXFLOAT centered:NO];
    [self.contentLayout addSubview:flowLayout];
    _flowLayout = flowLayout;
    
    
    NSMutableArray* btns = [NSMutableArray array];
    
    for (int i = 0; i < self.btnInfos.count; i++) {
        NSDictionary* btnInfo = self.btnInfos[i];
        RSVerButton* btn = [RSVerButton new];
        btn.gap = 8;
        btn.tag = i+100;
        [btn setImage:[UIImage imageNamed:btnInfo[@"image_n"]] forState:(UIControlStateNormal)];
        [btn setImage:[UIImage imageNamed:btnInfo[@"image_h"]] forState:(UIControlStateSelected)];
        [btn setTitle:btnInfo[@"title"] forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_GRAY_BASE forState:UIControlStateNormal];
        [btn setTitleColor:COLOR_GREEN forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(choiseType:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = FONTMID(15);
        btn.mySize = CGSizeMake(50, 72);
        [flowLayout addSubview:btn];
        [btns addObject:btn];
    }
    self.btns = btns;
    
    MyLinearLayout* infoLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    infoLayout.myTop = 32;
    infoLayout.myHeight = 60;
    infoLayout.myHorzMargin = 0;
    infoLayout.subviewHSpace = 20;
    infoLayout.padding = UIEdgeInsetsMake(0, 20, 0, 20);
    infoLayout.topBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE thick:1.0];
    infoLayout.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE thick:1.0];
    [self.contentLayout addSubview:infoLayout];
    
    UIImageView* typeIcon = [[UIImageView alloc] init];
    typeIcon.myCenterY = typeIcon.myLeft = 0;
    [infoLayout addSubview:typeIcon];
    _typeIcon = typeIcon;
    
    UILabel* typeLb = [UILabel lableWithFont:FONT(15) color:COLOR_BLACK_DARK text:@""];
    typeLb.myCenterY = 0;
    [infoLayout addSubview:typeLb];
    _typeLb = typeLb;
    
    NSAttributedString *placeholder1 = [[NSAttributedString alloc] initWithString:@"请输入生活收入金额" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    
    UITextField* amtTextField = [[UITextField alloc] init];
    amtTextField.attributedPlaceholder = placeholder1;
    amtTextField.borderStyle = UITextBorderStyleNone;
    amtTextField.keyboardType = UIKeyboardTypeDecimalPad;
    amtTextField.returnKeyType = UIReturnKeyDone;
    amtTextField.tintColor = COLOR_GREEN;
    amtTextField.textAlignment = NSTextAlignmentRight;
    amtTextField.font = FONT(15);
    amtTextField.textColor = COLOR_GREEN;
    amtTextField.delegate = self;
    amtTextField.myRight = amtTextField.myCenterY = 0;
    amtTextField.weight = 1.0;
    amtTextField.myHeight = 40;
    [infoLayout addSubview:amtTextField];
    _amtTextField = amtTextField;
    
    MyLinearLayout* remarkLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    remarkLayout.myHeight = 50;
    remarkLayout.myHorzMargin = 0;
    remarkLayout.subviewHSpace = 16;
    remarkLayout.padding = UIEdgeInsetsMake(0, 20, 0, 20);
    remarkLayout.bottomBorderline = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE thick:1.0];
    [self.contentLayout addSubview:remarkLayout];
    
    UILabel* remarkLb = [UILabel lableWithFont:FONT(15) color:COLOR_GRAY_BASE text:@"备注:"];
    remarkLb.myCenterY = 0;
    [remarkLayout addSubview:remarkLb];
    
    NSAttributedString *placeholder2 = [[NSAttributedString alloc] initWithString:@"请输入备注信息" attributes: @{NSForegroundColorAttributeName:COLOR_GRAY_QUITE}];
    UITextField* remarkTextField = [[UITextField alloc] init];
    remarkTextField.attributedPlaceholder = placeholder2;
    remarkTextField.textAlignment = NSTextAlignmentRight;
    remarkTextField.borderStyle = UITextBorderStyleNone;
    remarkTextField.returnKeyType = UIReturnKeyDone;
    remarkTextField.tintColor = COLOR_GREEN;
    remarkTextField.font = FONT(15);
    remarkTextField.delegate = self;
    remarkTextField.myCenterY = 0;
    remarkTextField.weight = 1.0;
    remarkTextField.myHeight = 32;
    [remarkLayout addSubview:remarkTextField];
    _remarkTextField = remarkTextField;
    
}

- (void)choiseType:(UIButton*)sender{
    for (UIButton* btn in self.btns) {
        btn.selected = NO;
    }
    sender.selected = YES;
    
    NSInteger index = sender.tag - 100;
    NSDictionary* btnInfo = self.btnInfos[index];
    _info = btnInfo;
    
    self.typeIcon.image = [UIImage imageNamed:btnInfo[@"image_h"]];
    [self.typeIcon sizeToFit];
    
    self.typeLb.text = btnInfo[@"title"];
    [self.typeLb sizeToFit];
}


#pragma mark  -- UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if(textField == self.amtTextField){
        [self.remarkTextField becomeFirstResponder];
        
    }else if(textField == self.remarkTextField){
        
        if([self.amtTextField.text floatValue] == 0){
            [Toast toast:@"请输入生活收入"];
            return YES;
        }
        
        if(self.remarkTextField.text.length == 0){
            [Toast toast:@"请输入备注信息"];
            return YES;
        }
        
        XDynamicModel* model = [XDynamicModel new];
        model.id = [NSDate date].timeIntervalSince1970;
        model.date = [NSDate date];
        model.type = XDynamicTypeBookKeeping;
        model.feeType = [_info[@"type"] integerValue];
        model.feeAmt = [self.amtTextField.text floatValue];
        model.title = _info[@"title"];
        model.content = self.remarkTextField.text;
        [[RLMRealm defaultRealm] transactionWithBlock:^{
            [[RLMRealm defaultRealm] addOrUpdateObject:model];
        }];
        [self closeViewControllerAnimated:YES];
    }
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
